package com.gohome.gohomechat.model;

import android.os.Handler;
import android.util.Log;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.gohome.gohomechat.db.GoHomeDBTools;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.model.entify.Notice;
import com.gohome.gohomechat.model.modellistener.AddFriendModelInterface;
import com.gohome.gohomechat.presenter.callback.AddFriendCallBack;
import com.gohome.gohomechat.presenter.callback.FindFriendCallBack;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/2.
 */
public class AddFriendModel implements AddFriendModelInterface {
    private Handler handler;

    @Override
    public void addFriend(final String userName, final String reason, final AddFriendCallBack callBack) {
        this.handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                ParseQuery<ParseObject> pq = ParseQuery.getQuery("AllUserGoHome");
                pq.whereEqualTo("username", userName);
                pq.getFirstInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject object, ParseException e) {
                        if (e == null) {
                            try {
                                List<Friend> datas = new ArrayList<>();
                                ParseFile file = object.getParseFile("head_img");
                                String url = file.getUrl();
                                EMContactManager.getInstance().addContact(userName, reason);//需异步处理
                                Notice notice = new Notice();
                                notice.setUserName(userName);
                                notice.setReason(reason);
                                notice.setHead(url);
                                notice.setMessageStatus(Contant.MESSAGESTATUS_NVITEED);
                                GoHomeDBTools.getInstance().insert(notice);
                                callBack.addFriendSuccess();
                            } catch (EaseMobException e1) {
                                callBack.addFriendFailed();
                            }
                        } else {
                            callBack.addFriendFailed();
                        }
                    }
                });
            }
        });
    }


    @Override
    public void findFriend(final String userName, final FindFriendCallBack callBack) {
        ParseQuery<ParseObject> pq = ParseQuery.getQuery("AllUserGoHome");
        pq.whereEqualTo("username", userName);
        pq.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    List<Friend> datas = new ArrayList<>();
                    ParseFile file = object.getParseFile("head_img");
                    Log.i("wcl", userName + "<<->>" );
                    String url = file.getUrl();
                    Friend friend = new Friend();
                    friend.setUserName(userName);
                    friend.setHead(url);
                    datas.add(friend);
                    callBack.findFriendSuccess(datas);
                } else {
                    callBack.findFriendFailed();
                }


            }
        });
    }
}